package com.fawvwebanma.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fawvwebanma.api.UserSearchApi;
import com.fawvwebanma.dto.ResultData;
import com.fawvwebanma.dto.TrainDto;
import com.fawvwebanma.mapper.TrainMapper;
import com.fawvwebanma.mapper.TrainPassStationMapper;
import com.fawvwebanma.util.TrainSearchUtil;

import lombok.extern.slf4j.Slf4j;

@Component
@DubboService
@Slf4j
public class UserSearchService implements UserSearchApi {

    @Autowired
    TrainPassStationMapper trainPassStationMapper;
    @Autowired
    TrainMapper trainMapper;
    @Autowired
    TrainSearchUtil trainSearchUtil;

    /**
     * 按照出发地、到达地查询车次，返回车次和途径地信息
     * 
     * @param leaveStationName
     * @param arriveStationName
     * @return
     */
    @Override
    public ResultData<List<TrainDto>> getTrainByLeaveAndArriveStations(String leaveStationName,
        String arriveStationName) {
        log.info("UserSearchService -->> getTrainByLeaveAndArriveStations");
        log.info("用户查询从" + leaveStationName + "到" + arriveStationName + "的列车车次");
        List<TrainDto> trainDtos =
            trainSearchUtil.getTrainByLeaveAndArriveStations(leaveStationName, arriveStationName);
        if (trainDtos.size() == 0) {
            log.info("没有找到相应的车次...............................");
        } else {
            log.info("找到了" + trainDtos.size() + "辆从" + leaveStationName + "到" + arriveStationName + "的车次");
            for (TrainDto trainDto : trainDtos) {
                log.info(trainDto.toString());
            }
        }
        return ResultData.success(trainDtos);
    }

    /**
     * 按照日期、出发地、到达地查询车次，返回车次和途径地信息
     * 
     * @param leaveStation
     * @param arriveStation
     * @param date
     * @return
     */
    @Override
    public ResultData<List<TrainDto>> getTrainByLeaveAndArriveStationsAndDate(String leaveStation, String arriveStation,
        String date) {
        log.info("UserSearchService -->> getTrainByLeaveAndArriveStationsAndDate");
        List<TrainDto> trainDtosNoDate = trainSearchUtil.getTrainByLeaveAndArriveStations(leaveStation, arriveStation);
        List<TrainDto> trainDtos = new ArrayList<>();
        log.info("用户查询" + date + "从" + leaveStation + "到" + arriveStation + "的列车车次");
        for (TrainDto trainDto : trainDtosNoDate) {
            TrainDto train = trainMapper.getTrainOnDate(trainDto.getTrainNum(), date);
            if (train != null) {
                trainDtos.add(trainDto);
            }
        }
        if (trainDtos.size() == 0) {
            log.info("没有找到" + date + "从" + leaveStation + "到" + arriveStation + "的车次...............................");
        } else {
            log.info("找到了" + trainDtos.size() + "辆从" + leaveStation + "到" + arriveStation + "的车次,出发日期：" + date);
            for (TrainDto trainDto : trainDtos) {
                log.info(trainDto.toString());
            }
        }
        return ResultData.success(trainDtos);
    }
}
